package com.pair.micro.common.response;

import com.pair.micro.common.constant.MicroBusinessError;
import com.pair.micro.common.constant.MicroSystemError;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * 响应数据
 *
 * @author xubz
 */
@Data
@Schema(description = "响应")
public class MicroResult<T> {
    @Schema(description = "编码 0表示成功，其他值表示失败")
    private String code = "0";

    @Schema(description = "消息内容")
    private String msg = "success";

    @Schema(description = "响应数据")
    private T data;

    public static <T> MicroResult<T> ok() {
        return ok(null);
    }

    public static <T> MicroResult<T> ok(T data) {
        MicroResult<T> microResult = new MicroResult<>();
        microResult.setData(data);
        return microResult;
    }

    public static <T> MicroResult<T> error() {
        return error(MicroSystemError.SYS_00002.getMsg());
    }

    public static <T> MicroResult<T> error(String msg) {
        return error(MicroSystemError.SYS_00002.getCode(), msg);
    }

    public static <T> MicroResult<T> error(MicroSystemError errorCode) {
        return error(errorCode.getCode(), errorCode.getMsg());
    }

    public static <T> MicroResult<T> error(String code, String msg) {
        MicroResult<T> microResult = new MicroResult<>();
        microResult.setCode(code);
        microResult.setMsg(msg);
        return microResult;
    }

    public static <T> MicroResult<T> BisError(MicroBusinessError microBusinessError) {
        MicroResult<T> microResult = new MicroResult<>();
        microResult.setCode(microBusinessError.getCode());
        microResult.setMsg(microBusinessError.getMsg());
        return microResult;
    }

    /**
     * 快速构建业务异常对象
     *
     * @param microBusinessError
     * @param microBusinessErrorMsg
     * @return
     */
    public static <T> MicroResult<T> BisError(
            MicroBusinessError microBusinessError, String microBusinessErrorMsg) {
        MicroResult<T> microResult = new MicroResult<>();
        microResult.setCode(microBusinessError.getCode());
        microResult.setMsg(microBusinessErrorMsg);
        return microResult;
    }

    /**
     * 快速构建系统异常对象
     *
     * @param microSystemError
     * @return
     */
    public static <T> MicroResult<T> SysError(MicroSystemError microSystemError) {
        MicroResult<T> microResult = new MicroResult<>();
        microResult.setCode(microSystemError.getCode());
        microResult.setMsg(microSystemError.getMsg());
        return microResult;
    }

    /**
     * 快速构建系统异常对象
     *
     * @param microSystemError
     * @param microSystemErrorMsg
     * @return
     */
    public static <T> MicroResult<T> SysError(
            MicroSystemError microSystemError, String microSystemErrorMsg) {
        MicroResult<T> microResult = new MicroResult<>();
        microResult.setCode(microSystemError.getCode());
        microResult.setMsg(microSystemErrorMsg);
        return microResult;
    }
}
